/*
 * SenderReportPanel.java
 *
 * Created on June 28, 2008, 3:41 PM
 */
package pl.edu.agh.ssm.monitor.gui;

import java.util.LinkedList;
import java.util.List;
import pl.edu.agh.ssm.monitor.data.SenderReportInfo;
import pl.edu.agh.ssm.monitor.data.SessionNode;
import pl.edu.agh.ssm.monitor.utils.NTPUtils;

/**
 *
 * @author  aneezka
 */
public class SenderReportPanel extends javax.swing.JPanel implements UpdatePanel {

	private static final long serialVersionUID = 1L;
	/** Creates new form SenderReportPanel */
    public SenderReportPanel(SessionNode node) {
        this.node = node;
        initComponents();
        update();
    }

    @Override
    public String getName() {
        return "Sender Report";
    }

    public void update() {
        SenderReportInfo report = node.getSenderInfo();
        if (report != null) {
            String ntp = "" + report.getNtp1TimeStamp() + "." + report.getNtp2TimeStamp();
            ntpLabel.setText(ntp + " (" + NTPUtils.ntpToDate(ntp) + ")");
            octetsLabel.setText("" + report.getTotalOctets() + " [B]");
            packetsLabel.setText("" + report.getTotalPackets());
            rtpLabel.setText("" + report.getRtpTimeStamp());
            for (UpdatePanelListener l : listeners) {
                l.panelUpdated(this);
            }
        } else {
            ntpLabel.setText(null);
            octetsLabel.setText(null);
            packetsLabel.setText(null);
            rtpLabel.setText(null);
            this.remove();
        }
    }

    public void remove() {
        for (UpdatePanelListener l : listeners) {
            l.panelRemoved(this);
        }
    }

    public void addListener(UpdatePanelListener listener) {
        listeners.add(listener);
    }

    public void removeListener(UpdatePanelListener listener) {
        listeners.remove(listener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        ntpLabel = new javax.swing.JLabel();
        rtpLabel = new javax.swing.JLabel();
        packetsLabel = new javax.swing.JLabel();
        octetsLabel = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pl.edu.agh.ssm.monitor.SessionMonitorApp.class).getContext().getResourceMap(SenderReportPanel.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        ntpLabel.setText(resourceMap.getString("ntpLabel.text")); // NOI18N
        ntpLabel.setName("ntpLabel"); // NOI18N

        rtpLabel.setText(resourceMap.getString("rtpLabel.text")); // NOI18N
        rtpLabel.setName("rtpLabel"); // NOI18N

        packetsLabel.setText(resourceMap.getString("packetsLabel.text")); // NOI18N
        packetsLabel.setName("packetsLabel"); // NOI18N

        octetsLabel.setText(resourceMap.getString("octetsLabel.text")); // NOI18N
        octetsLabel.setName("octetsLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(octetsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                    .addComponent(packetsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                    .addComponent(rtpLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                    .addComponent(ntpLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(ntpLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(rtpLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(packetsLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(octetsLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    private SessionNode node;
    private List<UpdatePanelListener> listeners = new LinkedList<UpdatePanelListener>();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel ntpLabel;
    private javax.swing.JLabel octetsLabel;
    private javax.swing.JLabel packetsLabel;
    private javax.swing.JLabel rtpLabel;
    // End of variables declaration//GEN-END:variables
}
